package com.driver.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.driver.enums.ResponseCode;
import com.driver.pojo.entity.User;
import com.driver.pojo.response.R;
import com.driver.service.IUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
@Slf4j
public class UserController {

    private final IUserService userService;

    // 登陆，支持用户名/手机号/邮箱登录
    @PostMapping("/login")
    public R<User> login(@RequestBody User user) {
        log.info("UserController login user:{}", user.toString());
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getName, user.getName());
        wrapper.eq(User::getPassword, user.getPassword());
        User one = userService.getOne(wrapper, false);
        if (one != null) {
            one.setPassword("");
            return R.success(one);
        }
        wrapper.clear();
        wrapper.eq(User::getTelephone, user.getTelephone());
        wrapper.eq(User::getPassword, user.getPassword());
        one = userService.getOne(wrapper, false);
        if (one != null) {
            one.setPassword("");
            return R.success(one);
        }
        wrapper.clear();
        wrapper.eq(User::getEmail, user.getEmail());
        wrapper.eq(User::getPassword, user.getPassword());
        one = userService.getOne(wrapper, false);
        if (one != null) {
            one.setPassword("");
            return R.success(one);
        }
        return R.fail(ResponseCode.ERROR_USER_PASSWORD);
    }

}
